package com.itxiuyixiu.homework.entity.dao;

import com.itxiuyixiu.homework.entity.model.Student;
import com.itxiuyixiu.homework.entity.vo.StudentVo;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author 黄磊
 * @date 2020/3/5
 */
public interface StudentDao extends JpaRepository<Student, String>, JpaSpecificationExecutor<Student> {
    boolean existsByAcademyId(int academyId);

    boolean existsByMajorId(int majorId);

    @Transactional(readOnly = true)
    @Query(value = "select studentId , birthday, gmtCreated, gmtModified, identityCard, sex, startYear, studentClass, studentContact, studentName, fk_academy_id as academyId, fk_major_Id as majorId from Student where studentId = ?1", nativeQuery = true)
    List<StudentVo> findAllByStudentId(String studentId);

    @Transactional(readOnly = true)
    @Query(value = "select studentId , birthday, gmtCreated, gmtModified, identityCard, sex, startYear, studentClass, studentContact, studentName, fk_academy_id as academyId, fk_major_Id as majorId from Student where studentName = ?1", nativeQuery = true)
    List<StudentVo> findAllByStudentName(String studentName);

    @Transactional(readOnly = true)
    @Query(value = "select studentId , birthday, gmtCreated, gmtModified, identityCard, sex, startYear, studentClass, studentContact, studentName, fk_academy_id as academyId, fk_major_Id as majorId from Student where identityCard = ?1", nativeQuery = true)
    List<StudentVo> findAllByIdentityCard(String identityCard);

    @Transactional(readOnly = true)
    @Query(value = "select studentId , birthday, gmtCreated, gmtModified, identityCard, sex, startYear, studentClass, studentContact, studentName, fk_academy_id as academyId, fk_major_Id as majorId from Student where fk_academy_id = ?1", nativeQuery = true)
    List<StudentVo> findAllByAcademyId(int academyId);

    @Transactional(readOnly = true)
    @Query(value = "select studentId , birthday, gmtCreated, gmtModified, identityCard, sex, startYear, studentClass, studentContact, studentName, fk_academy_id as academyId, fk_major_Id as majorId from Student", nativeQuery = true)
    List<StudentVo> findAllStudents();
}
